//
//  MServerDAO.m
//  MClient
//
//  Created by  史正烨 on 09-10-3.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "MServerDAO.h"
#import "MServerJsonDAO.h"
#import "MServerConfigCache.h"
#import "MCFunction.h"
#import "MServerStreamDAO.h"

static MServerDAO *mServerDAOInstance = nil;
static MServerJsonDAO *mServerJsonDAOInstance = nil;

@implementation MServerDAO

#pragma mark ---- singleton object methods ----

// See "Creating a Singleton Instance" in the Cocoa Fundamentals Guide for more info

+ (MServerDAO *)sharedInstance {
	@synchronized(self) {
		if (mServerDAOInstance == nil) {
			mServerDAOInstance = [[self alloc] init]; // assignment not done here
			//mServerJsonDAOInstance = [[MServerJsonDAO alloc] init];
		}
	}
	return mServerDAOInstance;
}

- (id)retain {
	return self;
}

+ (MServerJsonDAO*)jsonDAO {
	[mServerJsonDAOInstance release]; mServerJsonDAOInstance = nil;
	mServerJsonDAOInstance = [[MServerJsonDAO alloc] init];
	
	return mServerJsonDAOInstance;
}

- (unsigned)retainCount {
	return UINT_MAX;  // denotes an object that cannot be released
}

- (void)release {
	//do nothing
}

- (id)autorelease {
	return self;
}

#pragma mark ---- M-Server DAO methods ----

- (void)loginAsync:(NSString *)partyId 
			userId:(NSString *)userId 
		  password:(NSString *)password 
			  imei:(NSString *)imei 
			  imsi:(NSString *)imsi 
			 acode:(NSString *)acode
		  delegate:(id<MCServer2ViewDelegate>)delegate {
	[[MServerDAO jsonDAO] loginAsync:partyId
							  userId:userId
							password:password
								imei:imei
								imsi:imsi
							   acode:acode
							delegate:delegate];
}

- (id)login:(NSString *)partyId 
	 userId:(NSString *)userId 
   password:(NSString *)password 
	   imei:(NSString *)imei 
	   imsi:(NSString *)imsi
	  acode:(NSString *)acode {
	return [[MServerDAO jsonDAO] login:partyId userId:userId password:password imei:imei imsi:imsi acode:acode];
}

- (void)logoutAsync {
	[[MServerDAO jsonDAO] logoutAsync];
}

- (void)getAppsAsync:(id<MCServer2ViewDelegate>)delegate {
	id returnObject = [[MServerConfigCache sharedInstance] getApps];
	if (returnObject) {
		MCLog(@"getApps cached.");
		if (delegate && [delegate respondsToSelector:@selector(getDataFromServerSuccess:)]) {
			[delegate getDataFromServerSuccess:(NSMutableDictionary*)returnObject];
		}
		
	}else {
		MServerJsonDAO* jsonDao = [MServerDAO jsonDAO];
		jsonDao.cacheType = @"Apps:";
		[jsonDao getAppsAsync:delegate];
	}
}

- (id)getMenuOfApp:(NSString *)appId {
	id returnObject = [[MServerConfigCache sharedInstance] getMenuOfApp:appId];
	if (returnObject) {
		MCLog(@"getMenuOfApp cached.");
		return returnObject;
	}
	returnObject = [[MServerDAO jsonDAO] getMenuOfApp:appId];
	if (returnObject) {
		[[MServerConfigCache sharedInstance] addMenu:returnObject forKey:[NSString stringWithFormat:@"MenuOfApp:%@",appId]];
	}
	return returnObject;
}

- (void)getMenuOfAppAsync:(NSString *)appId 
			  otherParams:(NSMutableDictionary *)otherParams
				 delegate:(id<MCServer2ViewDelegate>)delegate {
	id returnObject = [[MServerConfigCache sharedInstance] getMenuOfApp:appId];
	if (returnObject) {
		MCLog(@"getMenuOfApp cached.");
		if (delegate && [delegate respondsToSelector:@selector(getDataFromServerSuccess:)]) {
			[delegate getDataFromServerSuccess:(NSMutableDictionary*)returnObject];
		}
		
	}else {
		MServerJsonDAO* jsonDao = [MServerDAO jsonDAO];
		jsonDao.cacheType = @"MenuOfApp:";
		[jsonDao getMenuOfAppAsync:appId
					   otherParams:otherParams
						  delegate:delegate];
	}
}

- (id)getSubMenu:(NSString *)menuId {
	id returnObject = [[MServerConfigCache sharedInstance] getSubMenu:menuId];
	if (returnObject) {
		MCLog(@"getSubMenu cached.");
		return returnObject;
	}
	returnObject = [[MServerDAO jsonDAO] getSubMenu:menuId];
	if (returnObject) {
		[[MServerConfigCache sharedInstance] addMenu:returnObject forKey:[NSString stringWithFormat:@"SubMenu:%@",menuId]];
	}
	return returnObject;
}

- (void)getSubMenuAsync:(NSString *)menuId 
			otherParams:(NSMutableDictionary *)otherParams
			   delegate:(id<MCServer2ViewDelegate>)delegate {
	id returnObject = [[MServerConfigCache sharedInstance] getSubMenu:menuId];
	if (returnObject) {
		MCLog(@"getSubMenu cached.");
		if (delegate && [delegate respondsToSelector:@selector(getDataFromServerSuccess:)]) {
			[delegate getDataFromServerSuccess:(NSMutableDictionary*)returnObject];
		}
	}else {
		MServerJsonDAO* jsonDao = [MServerDAO jsonDAO];
		jsonDao.cacheType = @"SubMenu:";
		[jsonDao getSubMenuAsync:menuId
					 otherParams:otherParams
						delegate:delegate];
	}
}

- (void)getMenuConfigAsync:(NSString *)menuId
				  delegate:(id<MCServer2ViewDelegate>)delegate {
	[[MServerDAO jsonDAO] getMenuConfigAsync:menuId
									delegate:delegate];
}

- (id)getListConfig:(NSString *)dataObjectName 
		 layoutName:(NSString *)layoutName {
	id returnObject = [[MServerConfigCache sharedInstance] getListConfig:dataObjectName layoutName:layoutName];
	if (returnObject) {
		MCLog(@"getListConfig cached.");
		return returnObject;
	}
	returnObject = [[MServerDAO jsonDAO] getListConfig:dataObjectName layoutName:layoutName];
	if (returnObject) {
		[[MServerConfigCache sharedInstance] addListLayout:returnObject
													forKey:[NSString stringWithFormat:@"%@:%@:%@", @"ListConfig:", dataObjectName, layoutName]];
	}
	return returnObject;
}

- (void)getListConfigAsync:(NSString *)dataObjectName 
				layoutName:(NSString *)layoutName 
			   otherParams:(NSMutableDictionary *)otherParams
				  delegate:(id<MCServer2ViewDelegate>)delegate {
	id returnObject = [[MServerConfigCache sharedInstance] getListConfig:dataObjectName layoutName:layoutName];
	if (returnObject) {
		MCLog(@"getListConfig cached.");
		if (delegate && [delegate respondsToSelector:@selector(getDataFromServerSuccess:)]) {
			[delegate getDataFromServerSuccess:(NSMutableDictionary*)returnObject];
		}
	}else {
		MServerJsonDAO* jsonDao = [MServerDAO jsonDAO];
		jsonDao.cacheType = @"ListConfig:";
		[jsonDao getListConfigAsync:dataObjectName 
						 layoutName:layoutName 
						otherParams:otherParams
						   delegate:delegate];		
	}
}

- (id)getListData:(NSString *)dataObjectName 
	   layoutName:(NSString *)layoutName 
	  queryFilter:(NSMutableArray *)queryFilter
			 page:(int)page {
	id returnObject = [[MServerDAO jsonDAO] getListData:dataObjectName layoutName:layoutName queryFilter:queryFilter page:page];
	return returnObject;
}

- (void)getListDataAsync:(NSString *)dataObjectName 
			  layoutName:(NSString *)layoutName
			 queryFilter:(NSMutableArray *)queryFilter 
					page:(int)page 
				delegate:(id<MCServer2ViewDelegate>)delegate {
	[[MServerDAO jsonDAO] getListDataAsync:dataObjectName 
								layoutName:layoutName 
							   queryFilter:queryFilter 
									  page:page
								  delegate:delegate];
}

- (id)getFormConfig:(NSString *)dataObjectName 
		 layoutName:(NSString *)layoutName 
		   editable:(NSString *)editable {
	id returnObject = [[MServerConfigCache sharedInstance] getFormConfig:dataObjectName layoutName:layoutName editable:editable];
	if (returnObject) {
		MCLog(@"getFormConfig cached.");
		return returnObject;
	}
	returnObject = [[MServerDAO jsonDAO] getFormConfig:dataObjectName layoutName:layoutName editable:editable];
	if (returnObject) {
		[[MServerConfigCache sharedInstance] addFormLayout:returnObject
													forKey:[NSString stringWithFormat:@"%@:%@:%@:%@", @"FormConfig:", dataObjectName, layoutName, editable]];
	}
	return returnObject;
}

- (void)getFormConfigAsync:(NSString *)dataObjectName 
				layoutName:(NSString *)layoutName 
				  editable:(NSString *)editable 
			   otherParams:(NSMutableDictionary *)otherParams
				  delegate:(id<MCServer2ViewDelegate>)delegate {
	id returnObject = [[MServerConfigCache sharedInstance] getFormConfig:dataObjectName layoutName:layoutName editable:editable];
	if (returnObject) {
		MCLog(@"getFormConfig cached.");
		if (delegate && [delegate respondsToSelector:@selector(getDataFromServerSuccess:)]) {
			[delegate getDataFromServerSuccess:(NSMutableDictionary*)returnObject];
		}
	}else {
		MServerJsonDAO* jsonDao = [MServerDAO jsonDAO];
		jsonDao.cacheType = @"FormConfig:";
		[jsonDao getFormConfigAsync:dataObjectName 
						 layoutName:layoutName 
						   editable:editable
						otherParams:otherParams
						   delegate:delegate];
	}
}

- (id)getFormData:(NSString *)dataObjectName
	   layoutName:(NSString *)layoutName 
			  key:(NSString *)key
	   isEditable:(NSString *)editable {
	id returnObject = [[MServerDAO jsonDAO] getFormData:dataObjectName layoutName:layoutName key:key isEditable:editable];
	return returnObject;
}

- (void)getFormDataAsync:(NSString *)dataObjectName 
			  layoutName:(NSString *)layoutName
					 key:(NSString *)key 
			  isEditable:(NSString *)editable
				delegate:(id<MCServer2ViewDelegate>)delegate {
	[[MServerDAO jsonDAO] getFormDataAsync:dataObjectName 
								layoutName:layoutName 
									   key:key 
								isEditable:editable
								  delegate:delegate];
}


- (id)saveFormData:(NSString *)buttonId 
		buttonType:(NSString *)buttonType
	dataObjectName:(NSString *)dataObjectName 
		  formData:(NSMutableDictionary *)formData {
	id returnObject = [[MServerDAO jsonDAO] saveFormData:buttonId buttonType:buttonType dataObjectName:dataObjectName formData:formData];
	return returnObject;
}

- (void)saveFormDataAsync:(NSString *)buttonId 
			   buttonType:(NSString *)buttonType 
		   dataObjectName:(NSString *)dataObjectName 
				 formData:(NSMutableDictionary *)formData 
			  otherParams:(NSMutableDictionary *)otherParams
				 delegate:(id<MCServer2ViewDelegate>)delegate {
	[[MServerDAO jsonDAO] saveFormDataAsync:buttonId 
								 buttonType:buttonType 
							 dataObjectName:dataObjectName 
								   formData:formData
								otherParams:otherParams
								   delegate:delegate];
}

- (id)deleteData:(NSString *)buttonId 
	  buttonType:(NSString *)buttonType 
  dataObjectName:(NSString *)dataObjectName
			keys:(NSMutableArray *)keys {
	id returnObject = [[MServerDAO jsonDAO] deleteData:buttonId buttonType:buttonType dataObjectName:dataObjectName keys:keys];
	return returnObject;
}

- (void)deleteDataAsync:(NSString *)buttonId 
			 buttonType:(NSString *)buttonType 
		 dataObjectName:(NSString *)dataObjectName 
				   keys:(NSMutableArray *)keys 
			otherParams:(NSMutableDictionary *)otherParams
			   delegate:(id<MCServer2ViewDelegate>)delegate {
	[[MServerDAO jsonDAO] deleteDataAsync:buttonId 
							   buttonType:buttonType 
						   dataObjectName:dataObjectName 
									 keys:keys
							  otherParams:otherParams
								 delegate:delegate];
}

-  (id)doServiceByRecord:(NSString *)buttonId 
			  buttonType:(NSString *)buttonType 
		  dataObjectName:(NSString *)dataObjectName 
				 records:(NSMutableArray *)records {
	id returnObject = [[MServerDAO jsonDAO] doServiceByRecord:buttonId buttonType:buttonType dataObjectName:dataObjectName records:records];
	return returnObject;
}

- (void)doServiceByRecordAsync:(NSString *)buttonId 
					buttonType:(NSString *)buttonType 
				dataObjectName:(NSString *)dataObjectName 
					   records:(NSMutableArray *)records 
				  serviceDefId:(NSString *)serviceDefId 
				   otherParams:(NSMutableDictionary *)otherParams
					  delegate:(id<MCServer2ViewDelegate>)delegate {
	[[MServerDAO jsonDAO] doServiceByRecordAsync:buttonId 
									  buttonType:buttonType 
								  dataObjectName:dataObjectName 
										 records:records
									serviceDefId:serviceDefId
									 otherParams:otherParams
										delegate:delegate];
}

-  (void)calcAsync:(NSString *)lookupBack
	dataObjectName:(NSString *)dataObjectName 
		   records:(NSMutableArray *)records 
	  serviceDefId:(NSString *)serviceDefId 
		  delegate:(id<MCServer2ViewDelegate>)delegate {
	[[MServerDAO jsonDAO] calcAsync:lookupBack
					 dataObjectName:dataObjectName 
							records:records
					   serviceDefId:serviceDefId
						   delegate:delegate];
}

- (id)getTree:(NSString *)treeId 
  queryFilter:(NSMutableArray *)queryFilter {
	id returnObject = [[MServerDAO jsonDAO] getTree:treeId queryFilter:queryFilter];
	return returnObject;
}

- (void)getTreeAsync:(NSString *)treeId 
		 queryFilter:(NSMutableArray *)queryFilter 
			delegate:(id<MCServer2ViewDelegate>)delegate {
	[[MServerDAO jsonDAO] getTreeAsync:treeId
						   queryFilter:queryFilter
							  delegate:delegate];
}

-(void)getNodeAsync:(NSString *)nid 
			 treeId:(NSString *)treeId 
		queryFilter:(NSMutableArray *)queryFilter 
			  param:(NSMutableDictionary *)param 
		   delegate:(id<MCServer2ViewDelegate>)delegate {
	[[MServerDAO jsonDAO] getNodeAsync:nid 
								treeId:treeId 
						   queryFilter:queryFilter 
								 param:param 
							  delegate:delegate];
}

- (id)getReportConfig:(NSString *)reportName {
	id returnObject = [[MServerConfigCache sharedInstance] getReportConfig:reportName];
	if (returnObject) {
		MCLog(@"getReportConfig cached.");
		return returnObject;
	}
	returnObject = [[MServerDAO jsonDAO] getReportConfig:reportName];
	if (returnObject) {
		[[MServerConfigCache sharedInstance] addReportQueryLayout:returnObject forKey:[NSString stringWithFormat:@"%@:%@", @"ReportConfig:",reportName]];
	}
	return returnObject;
}

- (void)getReportConfigAsync:(NSString *)reportName 
				 otherParams:(NSMutableDictionary *)otherParams
					delegate:(id<MCServer2ViewDelegate>)delegate {
	
	id returnObject = [[MServerConfigCache sharedInstance] getReportConfig:reportName];
	if (returnObject) {
		MCLog(@"getReportConfig cached.");
		if (delegate && [delegate respondsToSelector:@selector(getDataFromServerSuccess:)]) {
			[delegate getDataFromServerSuccess:(NSMutableDictionary*)returnObject];
		}
	}
	else {
		MServerJsonDAO* jsonDao = [MServerDAO jsonDAO];
		jsonDao.cacheType = @"ReportConfig:";
		[jsonDao getReportConfigAsync:reportName
						  otherParams:otherParams
							 delegate:delegate];
	}
}

- (void)modifyPasswordAsync:(NSString *)oldPassword
				   password:(NSString *)password 
				   delegate:(id<MCServer2ViewDelegate>)delegate {
	
	[[MServerDAO jsonDAO] modifyPasswordAsync:(NSString *)oldPassword 
									 password:(NSString *)password
									 delegate:delegate];
}

- (void)downloadAsync:(NSString *)actionType 
			 dataFrom:(NSString *)dataFrom 
				param:(NSMutableDictionary *)param 
			 delegate:(id<MCServer2ViewDelegate>)delegate {
	[[MServerDAO jsonDAO] downloadAsync:actionType 
							   dataFrom:dataFrom 
								  param:param
							   delegate:delegate];
}

- (void)uploadAsync:(NSString *)data 
		   delegate:(id<MCServer2ViewDelegate>)delegate {
	[[MServerDAO jsonDAO] uploadAsync:data
							 delegate:delegate];
}

- (void)getUploadFileNameAsync:(id<MCServer2ViewDelegate>)delegate {
	[[MServerDAO jsonDAO] getUploadFileNameAsync:delegate];
}

- (void)uploadFileAsync:(NSString *)actionType 
			   location:(NSString *)location 
				   data:(NSInputStream *)data 
			   delegate:(id<MCServer2ViewDelegate>)delegate {
	MServerStreamDAO *mServerDAOInstance = [[[MServerStreamDAO alloc] init] autorelease];
	[mServerDAOInstance	uploadFileAsync:actionType 
							   location:location 
								   data:data 
							   delegate:delegate];
}

- (void)downloadFileAsync:(NSString *)actionType 
				 dataFrom:(NSString *)dataFrom 
				 location:(NSString *)location
				 fileName:(NSString *)fileName
					 page:(int)page
					 type:(NSString *)type
			 saveLocation:(NSString *)saveLocation 
				 delegate:(id<MCServer2ViewDelegate>)delegate {
	MServerStreamDAO *mServerDAOInstance = [[[MServerStreamDAO alloc] init] autorelease];
	[mServerDAOInstance	downloadFileAsync:actionType
								 dataFrom:dataFrom
								 location:location
								 fileName:fileName
									 page:page
									 type:type
							 saveLocation:saveLocation
								 delegate:delegate];
}

- (void)getCustomFormAsync:(NSString *)key 
				layoutName:(NSString *)layoutName 
			   queryFilter:(NSMutableArray *)queryFilter 
			   otherParams:(NSMutableDictionary *)otherParams
				  delegate:(id<MCServer2ViewDelegate>)delegate {
	[[MServerDAO jsonDAO] getCustomFormAsync:key
								  layoutName:layoutName
								 queryFilter:queryFilter
								 otherParams:otherParams
									delegate:delegate];
}

- (void)getCustomListAsync:(NSString *)layoutName 
			   queryFilter:(NSMutableArray *)queryFilter 
					  page:(NSString *)page 
			   otherParams:(NSMutableDictionary *)otherParams
				  delegate:(id<MCServer2ViewDelegate>)delegate {
	[[MServerDAO jsonDAO] getCustomListAsync:layoutName
								 queryFilter:queryFilter
										page:page
								 otherParams:otherParams
									delegate:delegate];
}

- (void)getContactsAsync:(id<MCServer2ViewDelegate>)delegate {
	[[MServerDAO jsonDAO] getContactsAsync:delegate];
}

- (void)downloadContact:(NSString *)contactId
				   page:(int)page
			   delegate:(id<MCServer2ViewDelegate>)delegate {
	[[MServerDAO jsonDAO] downloadContact:contactId
									 page:page
								 delegate:delegate];
}

- (void)uploadContact:(NSString *)contactId
			  records:(NSMutableArray *)records
			 delegate:(id<MCServer2ViewDelegate>)delegate {
	[[MServerDAO jsonDAO] uploadContact:contactId
								records:records
							   delegate:delegate];
}

- (void)getFileList:(NSString *)dataFrom 
		  fileParam:(NSMutableDictionary *)fileParam 
		   delegate:(id<MCServer2ViewDelegate>)delegate {
	[[MServerDAO jsonDAO] getFileList:dataFrom 
							fileParam:fileParam
							 delegate:delegate];
}

@end
